/*
 * Please leave this Copyright notice in your code if you use it
 * Written by Decebal Mihailescu [http://www.codeproject.com/script/articles/list_articles.asp?userid=634640]
 */
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration.Install;
using System.Windows.Forms.Design;
using System.Runtime.InteropServices;
using System.Diagnostics;
using System.IO;
using System.Security.Permissions;
using System.Windows.Forms;

//see http://vladsnotes.hrybok.com/CategoryView,category,MSI.aspx
namespace FreeTranslator
{
    [RunInstaller(true)]
    public partial class NativeInstaller : Installer
    {
        public NativeInstaller()
        {
            InitializeComponent();
        }
        //[DllImport("mscoree.dll", EntryPoint = "GetCORSystemDirectory", ExactSpelling = true, CharSet = System.Runtime.InteropServices.CharSet.Ansi, SetLastError = true)]
        //private static extern int GetCORSystemDirectory([MarshalAs(UnmanagedType.LPWStr)] System.Text.StringBuilder Buffer, int BufferLength, ref int Length);


        [SecurityPermission(SecurityAction.Demand)]
        public override void Rollback(System.Collections.IDictionary savedState)
        {
            base.Rollback(savedState);
            ExecuteNGen(true);
        }

        private void ExecuteNGen(bool bUninstall)
        {
            string args = this.Context.Parameters["Args"];

            if (string.IsNullOrEmpty(args))
            {
                throw new InstallException("No arguments specified");
            }

            Process proc = null;
            try
            {

                string ngenpath = Path.Combine(RuntimeEnvironment.GetRuntimeDirectory(), "ngen.exe");
                ProcessStartInfo si = new ProcessStartInfo(ngenpath,
                string.Format(" {0} \"{1}\"", bUninstall ? "uninstall" : "install", args));
                si.WorkingDirectory = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
                si.WindowStyle = ProcessWindowStyle.Hidden;

                proc = Process.Start(si);
                proc.WaitForExit();
            }
            catch (Exception e)
            {
                throw new InstallException(e.Message);
            }
            finally
            {
                // path.Length = 0;
            }
        }
        private bool IsUpgrade
        {
            get
            {
                return !string.IsNullOrEmpty(this.Context.Parameters["OldProductCode"]);
            }
        }

        protected override void OnCommitting(System.Collections.IDictionary savedState)
        {
            base.OnCommitting(savedState);
        }
        protected override void OnCommitted(System.Collections.IDictionary savedState)
        {
            base.OnCommitted(savedState);

        }
        protected override void OnBeforeInstall(System.Collections.IDictionary savedState)
        {
            base.OnBeforeInstall(savedState);
        }
        protected override void OnAfterInstall(System.Collections.IDictionary savedState)
        {
            base.OnAfterInstall(savedState);
        }
        [SecurityPermission(SecurityAction.Demand)]
        public override void Uninstall(System.Collections.IDictionary savedState)
        {
            base.Uninstall(savedState);
            ExecuteNGen(true);
        }

        [SecurityPermission(SecurityAction.Demand)]
        public override void Install(System.Collections.IDictionary savedState)
        {
            base.Install(savedState);
            ExecuteNGen(false);
        }

    }
}